import request from '/@/utils/request';

export const getDicts = (type: String) => {
	return request({
		url: `/admin/V1/dict/type/type`,
		method: 'post',
		data: { dictType: type },
	});
};

export function fetchList(data: any) {
	return request({
		url: '/admin/V1/dict/list',
		method: 'post',
		data: data,
	});
}

export function fetchItemList(data: any) {
	return request({
		url: '/admin/V1/dict/item/page',
		method: 'post',
		data: data,
	});
}

export function addItemObj(data: any) {
	return request({
		url: '/admin/V1/dict/add',
		method: 'post',
		data: data,
	});
}

export function getItemObj(id: string) {
	return request({
		url: '/admin/V1/dict/item/details',
		method: 'post',
		data: { id: id },
	});
}

export function getItemDetails(data: object) {
	return request({
		url: '/admin/V1/dict/item/add',
		method: 'post',
		data: data,
	});
}

export function delItemObj(data: string) {
	return request({
		url: '/admin/V1/dict/item/delete',
		method: 'post',
		data: { id: data },
	});
}

export function putItemObj(data: any) {
	return request({
		url: '/admin/V1/dict/item/update',
		method: 'post',
		data: data,
	});
}
export function addObjItem(obj: any) {
	return request({
		url: '/admin/V1/dict/item/add',
		method: 'post',
		data: obj,
	});
}

export function addObj(obj: any) {
	return request({
		url: '/admin/V1/dict/add',
		method: 'post',
		data: obj,
	});
}

export function getObj(id: string) {
	return request({
		url: '/admin/V1/dict/details/id',
		method: 'post',
		data: { id: id },
	});
}

export function getObjDetails(data: object) {
	return request({
		url: '/admin/V1/dict/type/type',
		method: 'post',
		data: data,
	});
}

export function delObj(ids: any) {
	return request({
		url: '/admin/V1/dict/deleted',
		method: 'post',
		data: { ids },
	});
}

export function putObj(data: any) {
	return request({
		url: '/admin/V1/dict/update',
		method: 'post',
		data: data,
	});
}

export function refreshCache() {
	return request({
		url: '/admin/V1/dict/sync',
		method: 'post',
	});
}

export function validateDictType(rule: any, value: any, callback: any, isEdit: boolean) {
	if (isEdit) {
		return callback();
	}

	getObjDetails({ dictType: value }).then((response) => {
		const result = response.data;
		if (result !== null) {
			callback(new Error('字典类型已经存在'));
		} else {
			callback();
		}
	});
}

export function validateDictItemLabel(rule: any, value: any, callback: any, type: string, isEdit: boolean) {
	if (isEdit) {
		return callback();
	}

	getItemDetails({ dictType: type, label: value }).then((response) => {
		const result = response.data;
		if (result !== null) {
			callback(new Error('标签已经存在'));
		} else {
			callback();
		}
	});
}
